//
//  CMDBSelectedImageCell.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/20.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CMDBSelectedImageCell: CMDBBaseCollectionViewCell {
    
    var coverImageView : UIImageView!
    
    var imageDescLabel : UITextField!
    
    var addButton : UIButton!
    
    var deleteButton : UIButton!
    
    var image : UIImage? {
        
        didSet{
            
            if image != nil {
                
                guard let image = image else { return }
                
                coverImageView.image             = image
                
                addButton.userInteractionEnabled = false
            }else{
                
                coverImageView.image             = nil
                
                addButton.userInteractionEnabled = true
                
                imageDescLabel.hidden            = true
                
                deleteButton.hidden              = true
                
            }
        }
    }
    
    override func buildCollectionCell() {
        super.buildCollectionCell()
        
        buildSubViews()
    }
    
    override func buildSubViews() {
        super.buildSubViews()
        
        deleteButton        = UIButton()
        
        deleteButton.setImage(UIImage(named: "发布-关闭-右上"), forState: .Normal)

        addButton           = UIButton()
        
        addButton.setBackgroundImage(UIImage(named: "上传照片-添加照片"), forState: .Normal)
        
        addButton.addTarget(self, action: #selector(addPhotoClick), forControlEvents: .TouchUpInside)
        
        addButton.sizeToFit()
        
        addButton.frame      = CGRect(x: 0, y: 0, width: width, height: width)
        
        coverImageView       = UIImageView()
        
        coverImageView.frame = CGRect(x: 0, y: 0, width: width, height: width)
        
        imageDescLabel       = UITextField()
        
        imageDescLabel.placeholder = "请添加描述"
        
        imageDescLabel.frame  = CGRect(x: 0, y: width, width: width, height: 30)
        
        contentView.addSubview(imageDescLabel)
        
        contentView.addSubview(addButton)
        
        contentView.addSubview(coverImageView)
        
        contentView.addSubview(deleteButton)
        
        deleteButton.snp_makeConstraints { (make) in
            make.right.equalTo(0);
            make.top.equalTo(0);
            make.width.equalTo(30);
            make.height.equalTo(30);
        }

        
    }
}


//MARK:- action methods
extension CMDBSelectedImageCell
{
    
    @objc func addPhotoClick(){
        
        mDelegate?.onItemClickType!("AddPhotoClick", data: nil, indexPath: nil)
        
    }
    
}




